﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using PointOfSale.Class;
using System.Data.SqlClient;
using System.Data;
using PointOfSale.Global;

namespace PointOfSale.PointOfSale
{
	/// <summary>
	/// Summary description for frmRuouPhaDacBiet.
	/// </summary>
	public class frmRuouPhaDacBiet : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox cboLoaiRuou;
		private System.Windows.Forms.TextBox txtSoLuong;
		private System.Windows.Forms.Label lblLoaiRuou;
		private System.Windows.Forms.Label lblSoLuong;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnThem;
		private System.Windows.Forms.Button btnXoa;
		private System.Windows.Forms.Button btnOK;
		private System.Windows.Forms.ListView lvsDanhSachThanhPhan;
		private System.Windows.Forms.ColumnHeader colItemId;
		private System.Windows.Forms.ColumnHeader colTenThanhPhan;
		private System.Windows.Forms.ColumnHeader colSoLuong;
		private System.Windows.Forms.ComboBox cboDonVi;
		private System.Windows.Forms.ColumnHeader colUnitId;
		private System.Windows.Forms.Label lblGiaTien;
		private System.Windows.Forms.TextBox txtGiaTien;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmRuouPhaDacBiet()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.cboLoaiRuou = new System.Windows.Forms.ComboBox();
			this.txtSoLuong = new System.Windows.Forms.TextBox();
			this.lblLoaiRuou = new System.Windows.Forms.Label();
			this.lblSoLuong = new System.Windows.Forms.Label();
			this.lvsDanhSachThanhPhan = new System.Windows.Forms.ListView();
			this.colItemId = new System.Windows.Forms.ColumnHeader();
			this.colTenThanhPhan = new System.Windows.Forms.ColumnHeader();
			this.colSoLuong = new System.Windows.Forms.ColumnHeader();
			this.colUnitId = new System.Windows.Forms.ColumnHeader();
			this.label1 = new System.Windows.Forms.Label();
			this.btnThem = new System.Windows.Forms.Button();
			this.btnXoa = new System.Windows.Forms.Button();
			this.btnOK = new System.Windows.Forms.Button();
			this.cboDonVi = new System.Windows.Forms.ComboBox();
			this.lblGiaTien = new System.Windows.Forms.Label();
			this.txtGiaTien = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// cboLoaiRuou
			// 
			this.cboLoaiRuou.Location = new System.Drawing.Point(64, 16);
			this.cboLoaiRuou.Name = "cboLoaiRuou";
			this.cboLoaiRuou.Size = new System.Drawing.Size(128, 21);
			this.cboLoaiRuou.TabIndex = 0;
			this.cboLoaiRuou.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboLoaiRuou_KeyDown);
			this.cboLoaiRuou.SelectedIndexChanged += new System.EventHandler(this.cboLoaiRuou_SelectedIndexChanged);
			// 
			// txtSoLuong
			// 
			this.txtSoLuong.Location = new System.Drawing.Point(264, 16);
			this.txtSoLuong.Name = "txtSoLuong";
			this.txtSoLuong.Size = new System.Drawing.Size(80, 20);
			this.txtSoLuong.TabIndex = 1;
			this.txtSoLuong.Text = "";
			this.txtSoLuong.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtSoLuong_KeyDown);
			// 
			// lblLoaiRuou
			// 
			this.lblLoaiRuou.AutoSize = true;
			this.lblLoaiRuou.Location = new System.Drawing.Point(8, 16);
			this.lblLoaiRuou.Name = "lblLoaiRuou";
			this.lblLoaiRuou.Size = new System.Drawing.Size(51, 16);
			this.lblLoaiRuou.TabIndex = 2;
			this.lblLoaiRuou.Text = "Loại rượu";
			// 
			// lblSoLuong
			// 
			this.lblSoLuong.AutoSize = true;
			this.lblSoLuong.Location = new System.Drawing.Point(200, 16);
			this.lblSoLuong.Name = "lblSoLuong";
			this.lblSoLuong.Size = new System.Drawing.Size(49, 16);
			this.lblSoLuong.TabIndex = 3;
			this.lblSoLuong.Text = "Số lượng";
			// 
			// lvsDanhSachThanhPhan
			// 
			this.lvsDanhSachThanhPhan.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																								   this.colItemId,
																								   this.colTenThanhPhan,
																								   this.colSoLuong,
																								   this.colUnitId});
			this.lvsDanhSachThanhPhan.FullRowSelect = true;
			this.lvsDanhSachThanhPhan.GridLines = true;
			this.lvsDanhSachThanhPhan.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.lvsDanhSachThanhPhan.HideSelection = false;
			this.lvsDanhSachThanhPhan.Location = new System.Drawing.Point(8, 72);
			this.lvsDanhSachThanhPhan.MultiSelect = false;
			this.lvsDanhSachThanhPhan.Name = "lvsDanhSachThanhPhan";
			this.lvsDanhSachThanhPhan.Size = new System.Drawing.Size(264, 152);
			this.lvsDanhSachThanhPhan.TabIndex = 4;
			this.lvsDanhSachThanhPhan.View = System.Windows.Forms.View.Details;
			// 
			// colItemId
			// 
			this.colItemId.Text = "itemId";
			this.colItemId.Width = 0;
			// 
			// colTenThanhPhan
			// 
			this.colTenThanhPhan.Text = "Tên thành phần";
			this.colTenThanhPhan.Width = 182;
			// 
			// colSoLuong
			// 
			this.colSoLuong.Text = "Số lượng (cc)";
			this.colSoLuong.Width = 77;
			// 
			// colUnitId
			// 
			this.colUnitId.Text = "unitId";
			this.colUnitId.Width = 0;
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(8, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(164, 16);
			this.label1.TabIndex = 5;
			this.label1.Text = "Danh sách các thành phần rượu";
			// 
			// btnThem
			// 
			this.btnThem.Location = new System.Drawing.Point(280, 48);
			this.btnThem.Name = "btnThem";
			this.btnThem.Size = new System.Drawing.Size(72, 23);
			this.btnThem.TabIndex = 6;
			this.btnThem.Text = "Thêm";
			this.btnThem.Click += new System.EventHandler(this.btnThem_Click);
			// 
			// btnXoa
			// 
			this.btnXoa.Location = new System.Drawing.Point(280, 80);
			this.btnXoa.Name = "btnXoa";
			this.btnXoa.Size = new System.Drawing.Size(72, 23);
			this.btnXoa.TabIndex = 7;
			this.btnXoa.Text = "Xóa";
			this.btnXoa.Click += new System.EventHandler(this.btnXoa_Click);
			// 
			// btnOK
			// 
			this.btnOK.Location = new System.Drawing.Point(328, 232);
			this.btnOK.Name = "btnOK";
			this.btnOK.Size = new System.Drawing.Size(80, 24);
			this.btnOK.TabIndex = 8;
			this.btnOK.Text = "OK";
			this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
			// 
			// cboDonVi
			// 
			this.cboDonVi.Location = new System.Drawing.Point(352, 16);
			this.cboDonVi.Name = "cboDonVi";
			this.cboDonVi.Size = new System.Drawing.Size(56, 21);
			this.cboDonVi.TabIndex = 9;
			this.cboDonVi.KeyDown += new System.Windows.Forms.KeyEventHandler(this.cboDonVi_KeyDown);
			// 
			// lblGiaTien
			// 
			this.lblGiaTien.AutoSize = true;
			this.lblGiaTien.Location = new System.Drawing.Point(16, 232);
			this.lblGiaTien.Name = "lblGiaTien";
			this.lblGiaTien.Size = new System.Drawing.Size(43, 16);
			this.lblGiaTien.TabIndex = 10;
			this.lblGiaTien.Text = "Giá tiền";
			// 
			// txtGiaTien
			// 
			this.txtGiaTien.Location = new System.Drawing.Point(72, 232);
			this.txtGiaTien.Name = "txtGiaTien";
			this.txtGiaTien.TabIndex = 11;
			this.txtGiaTien.Text = "";
			// 
			// frmRuouPhaDacBiet
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 261);
			this.Controls.Add(this.txtGiaTien);
			this.Controls.Add(this.lblGiaTien);
			this.Controls.Add(this.cboDonVi);
			this.Controls.Add(this.btnOK);
			this.Controls.Add(this.btnXoa);
			this.Controls.Add(this.btnThem);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.lvsDanhSachThanhPhan);
			this.Controls.Add(this.lblSoLuong);
			this.Controls.Add(this.lblLoaiRuou);
			this.Controls.Add(this.txtSoLuong);
			this.Controls.Add(this.cboLoaiRuou);
			this.Name = "frmRuouPhaDacBiet";
			this.Text = "Rượu pha đặc biệt";
			this.Load += new System.EventHandler(this.frmRuouPhaDacBiet_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmRuouPhaDacBiet_Load(object sender, System.EventArgs e)
		{
			//Lay danh sach cac loai ruou tu csdl dua vao cboLoaiRuou
			DataTable dtLoaiRuou = clsItem.DanhSachCacLoaiRuou();
			cboLoaiRuou.DataSource = dtLoaiRuou;
			cboLoaiRuou.ValueMember = "itemID";
			cboLoaiRuou.DisplayMember = "itemName";
			cboLoaiRuou.SelectedItem = this.cboLoaiRuou.Items[0];

			//Lay danh sach cac don vi tu csdl dua vao cboDonVi
			DataTable dtDonVi=clsUnit.GetAll();
			cboDonVi.DataSource=dtDonVi;
			cboDonVi.DisplayMember="UnitName";
			cboDonVi.ValueMember="UnitId";
		}

		public bool IsDecimal(string theValue)
		{
			try
			{
				Convert.ToDouble(theValue);
				return true;
			} 
			catch 
			{
				return false;
			}
		}

		private void btnThem_Click(object sender, System.EventArgs e)
		{
			//Kiem tra xem so luong nhap vao co phai la so hay khong
			if(!IsDecimal(this.txtSoLuong.Text))
			{
				MessageBox.Show("Số lượng nhập vào phải là số");
				return;
			}

			//Kiem tra xem so luong nay con du trong kho hay khong
			if(clsItemDepartment.IsEnough(Convert.ToInt32(cboLoaiRuou.SelectedValue), Convert.ToInt32(cboDonVi.SelectedValue), Convert.ToDouble(txtSoLuong.Text)))
			{
				MessageBox.Show("Số lượng mặt hàng này không còn đủ trong kho");
				return;
			}

			//Kiem tra xem thanh phan do da duoc them chua
			for( int j = 0; j < lvsDanhSachThanhPhan.Items.Count; j++)
			{
				if(lvsDanhSachThanhPhan.Items[j].SubItems[0].Text.Equals(cboLoaiRuou.SelectedValue.ToString()))
				{
					MessageBox.Show("Thành phần này đã có rồi");
					return;
				}
			}
		
			ListViewItem lvi = new ListViewItem(new String[]{cboLoaiRuou.SelectedValue.ToString(), cboLoaiRuou.Text, txtSoLuong.Text, cboDonVi.SelectedValue.ToString()});
			lvsDanhSachThanhPhan.Items.Add(lvi);
			this.cboLoaiRuou.Focus();
		}

		private void btnXoa_Click(object sender, System.EventArgs e)
		{
			if(lvsDanhSachThanhPhan.FocusedItem == null)
			{
				MessageBox.Show("Hãy chọn thành phần để xóa");
			}
			else
			{
				lvsDanhSachThanhPhan.FocusedItem.Remove();
			}
		}

		private void btnOK_Click(object sender, System.EventArgs e)
		{
			//Kiem tra xem da nhap gia tien chua
			if(!IsDecimal(txtGiaTien.Text))
			{
				MessageBox.Show("Giá tiền nhập vào phải là số khác 0");
				return;
			}
			
			//Lan luot tru so luong cua tung thanh phan trong kho hang
			for(int i = 0; i < lvsDanhSachThanhPhan.Items.Count; i++)
			{
				int intItemId=Convert.ToInt32(lvsDanhSachThanhPhan.Items[i].SubItems[0].Text);
				int intUnitId=Convert.ToInt32(lvsDanhSachThanhPhan.Items[i].SubItems[3].Text);
				double dbSoLuong=Convert.ToDouble(lvsDanhSachThanhPhan.Items[i].SubItems[2].Text);
				clsItemDepartment.UpdateQty("-", dbSoLuong, intItemId, intUnitId);
			}
			#region Ghi vao bang sale
			clsSale sale =new clsSale();
			sale.InvoiceDate =DateTime.Now.ToString();
			sale.DepartmentId = clsGlobal.g_DepartmentId;
			sale.Total = Convert.ToDouble(txtGiaTien.Text);
			sale.InvoiceAmount = Convert.ToDouble(txtGiaTien.Text);;
			sale.PaidAmount = Convert.ToDouble(txtGiaTien.Text);;
			sale.PostAmount = 0.0;
			sale.UserId = clsGlobal.g_UserId;
			sale.GuestId = 0;
			sale.Remark = "";
			sale.RoomNumber = "0";
			sale.IsFinished = true;
			sale.ShiftTypeId = 0;
			sale.IsReported = false;

			sale.Add();
			#endregion

			#region Ghi vao bang chi tiet ban & cap nhat hang hoa.
			clsSaleDetail saleDetail = new clsSaleDetail();
			saleDetail.SaleId = clsSale.GetLastSaleId();
			int n = lvsDanhSachThanhPhan.Items.Count;
			for( int i = 0; i < n; i ++)
			{
				//Ghi vao bang chi tiet.
				saleDetail.ItemId = Convert.ToInt32(lvsDanhSachThanhPhan.Items[i].SubItems[0].Text);
				saleDetail.Quantity = Convert.ToDouble(lvsDanhSachThanhPhan.Items[i].SubItems[2].Text);
				saleDetail.Rate = 0.0;
				saleDetail.UnitId = Convert.ToInt32(lvsDanhSachThanhPhan.Items[i].SubItems[3].Text);
				saleDetail.Amount = 0.0;
				saleDetail.Add();
			}
			#endregion
			this.Close();
		}

		private void cboLoaiRuou_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				txtSoLuong.Focus();
			}
		}

		private void txtSoLuong_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				cboDonVi.Focus();
			}
		}

		private void cboDonVi_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode == Keys.Enter)
			{
				this.btnThem.Focus();
			}
		}

		private void cboLoaiRuou_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if (this.cboLoaiRuou.SelectedValue.ToString().Length < 10) 
			{
				//Lay danh sach cac don vi tu csdl dua vao cboDonVi
				DataTable dtDonVi=clsUnitConversion.GetUnitForAnItem(Convert.ToInt32(cboLoaiRuou.SelectedValue));
				cboDonVi.DataSource=dtDonVi;
				cboDonVi.DisplayMember="unitName";
				cboDonVi.ValueMember="unitId";
				
				//Hien thi don vi mac dinh la cc
				int i = cboDonVi.FindString("cc");
				if(i > 0)
				{
					cboDonVi.SelectedItem=cboDonVi.Items[i];		
				}
			}
		}
	}
}
